//
//  CCTypePriceCell.m
//  CwinCar
//
//  Created by dlios on 15/10/19.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "CCTypePriceCell.h"
#import "BaseHeader.h"

#import "FindMapViewController.h"
@implementation CCTypePriceCell

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _i = 0 ;
        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * suitableScreen_Width, 5 * suitableScreen_Width, 200 * suitableScreen_Width, 40 * suitableScreen_Width)];
        [self.contentView addSubview:self.nameLabel];
        
        self.addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * suitableScreen_Width, 40 * suitableScreen_Width, 365 * suitableScreen_Width, 30 * suitableScreen_Width)];
        [self.contentView addSubview:self.addressLabel];
        self.addressLabel.alpha = 0.5 ;
        [self.addressLabel setFont:[UIFont systemFontOfSize:14]];
        self.distanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * suitableScreen_Width, 70 * suitableScreen_Width, 150 * suitableScreen_Width, 20 * suitableScreen_Width)];
        self.distanceLabel.alpha = 0.5 ;
        [self.contentView addSubview:self.distanceLabel];
        [self.distanceLabel setBackgroundColor:[UIColor colorWithRed:232/255. green:232/255. blue:232/255. alpha:1]];
        [self.distanceLabel setFont:[UIFont systemFontOfSize:14]];
        self.rangeLabel = [[UILabel alloc]initWithFrame:CGRectMake(180 * suitableScreen_Width, 70 * suitableScreen_Width, 65 * suitableScreen_Width, 20 * suitableScreen_Width)];
        [self.rangeLabel setFont:[UIFont systemFontOfSize:14]];
        self.rangeLabel.alpha = 0.5 ;
        [self.rangeLabel setBackgroundColor:[UIColor colorWithRed:232/255. green:232/255. blue:232/255. alpha:1]];
        [self.contentView addSubview:self.rangeLabel];
        [self.rangeLabel setTextAlignment:NSTextAlignmentCenter];
        
        
        self.priceLabel =[[UILabel alloc]initWithFrame:CGRectMake(275 * suitableScreen_Width, 5 * suitableScreen_Width, 100 * suitableScreen_Width, 40 * suitableScreen_Width)];
        [self.contentView addSubview:self.priceLabel];
        [self.priceLabel setTextColor:[UIColor redColor]];
        
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 99 * suitableScreen_Width, mainScreen_Width, 3 * suitableScreen_Width)];
        [self.contentView addSubview:view];
        [view setBackgroundColor:[UIColor colorWithRed:243/255. green:243/255. blue:243/255. alpha:1]];
        
    }
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    return self ;
}
-(void)frameChange{
    
    [self.addressLabel setFrame:CGRectMake(10* suitableScreen_Width - 0.1 * _i, 40 * suitableScreen_Width, wid, 30 * suitableScreen_Width)];
    
    if (self.addressLabel.frame.origin.x <= 0 - wid) {
        _i = -3650 ;
    }
    _i ++ ;
}
-(void)getTypeModel:(CCTypePriceModel *)model{
    
    [self.nameLabel setText:model.shortname];
    
    [self.addressLabel setText:model.address];
    NSInteger tex = model.address.length ;
    if (tex > mainScreen_Width/14) {
        [self.addressLabel setFrame:CGRectMake(10 * suitableScreen_Width, 40 * suitableScreen_Width, tex * 14, 30 * suitableScreen_Width)];
        wid = tex * 14 ;
        if (_maTimer == nil) {
            
            _maTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(frameChange) userInfo:nil repeats:YES];
        }
    }
    
    
    [self.priceLabel setText:[NSString stringWithFormat:@"%@万",model.price]];
    
    [self.rangeLabel setText:model.orderrange];
    FindMapViewController * map = [[FindMapViewController alloc]init];
    
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"distance"]isEqualToString:@"distance"]) {
        int i = 0 ;
        int sum = 1 ;
        while (i >=0) {
            sum = 2 * sum ;
            if (i == 0) {
                sum = 1 ;
            }
            if (sum * 3750>=model.distance) {
                if (i >19) {
                    i = 19 ;
                }
                if (i < 3) {
                    i = 3 ;
                }
                map.cale = 19 - i ;
                break ;
            }
            i = i +1 ;
        }
        
        
        [self.distanceLabel setText:[NSString stringWithFormat:@"与当前距离%.0f公里",(model.distance/1000)]];
    }else{
        [self.distanceLabel setText:@"未能显示与当前距离"];
    }
}

@end
